using AutoMapper;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ShopPowerTemplate.Entities;
using ShopPowerTemplate.IoC;
using ShopPowerTemplate.Presentation.Mvc.Controllers;
using ShopPowerTemplate.Presentation.Mvc.Extensions.BootStrapper;
using ShopPowerTemplate.Presentation.Mvc.ViewModels;
using ShopPowerTemplate.Services.Tests;
using System.Web.Mvc;

namespace ShopPowerTemplate.Presentation.Mvc.Test
{
    [TestClass]
    public class OptionTypeControllerTestBase
    {
        [TestInitialize]
        public virtual void TestInitialize()
        {
            IBootStrapper bootStrapper = new DefaultBootStrapper(IoCFactory.Instance.CurrentContainer);
            bootStrapper.Boot();
        }

        [TestMethod]
        public virtual void OptionTypeController_IndexTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<OptionTypeController>());

            var result = controller.Index() as ViewResult;
            
            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.IsTrue(string.IsNullOrEmpty(result.ViewName));
            Assert.IsTrue(result.Model is IndexOptionTypeViewModel);
        }

        [TestMethod]
        public virtual void OptionTypeController_LoadTest()
        {
            OptionTypeServiceUtil.CreateOptionType();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<OptionTypeController>());

            var result = controller.Load("", "", 0, 10, "") as JsonResult;

            var rows = ModelHelper.GetList(result.Data);
            Assert.IsTrue(rows.Count > 0);
        }

        [TestMethod]
        public virtual void OptionTypeController_DeleteTest()
        {
            OptionType optionType = OptionTypeServiceUtil.CreateOptionType();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<OptionTypeController>());

            var result = controller.Delete(optionType.Id) as ContentResult;

            Assert.IsNotNull(result);
            Assert.IsTrue(string.IsNullOrEmpty(result.Content));
        }

        [TestMethod]
        public virtual void OptionTypeController_ReportTest()
        {
            OptionType optionType = OptionTypeServiceUtil.CreateOptionType();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<OptionTypeController>());

            var result = controller.Report() as FileContentResult;

            Assert.IsNotNull(result);
            Assert.IsTrue(result.ContentType == "text/csv");
            Assert.IsNotNull(result.FileContents.Length > 0);
        }

        [TestMethod]
        public virtual void OptionTypeController_GetCreateTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<OptionTypeController>());

            var result = controller.Create() as ViewResult;

            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.IsTrue(string.IsNullOrEmpty(result.ViewName));
            Assert.IsTrue(result.Model is CreateOptionTypeViewModel);
        }

        [TestMethod]
        public virtual void OptionTypeController_PostCreateTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<OptionTypeController>());
            OptionType optionType = OptionTypeServiceUtil.GetOptionType();
            CreateOptionTypeViewModel viewModel = Mapper.Map<OptionType, CreateOptionTypeViewModel>(optionType);

            var result = controller.Create(viewModel) as RedirectToRouteResult;

            Assert.IsNotNull(result);
            Assert.AreEqual(result.RouteValues["action"], "Edit"); //Create redirect to edit action on success
            Assert.IsTrue((int)result.RouteValues["id"] > 0); //Check new item id
        }

        [TestMethod]
        public virtual void OptionTypeController_GetEditTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<OptionTypeController>());
            OptionType optionType = OptionTypeServiceUtil.CreateOptionType();

            var result = controller.Edit(optionType.Id) as ViewResult;

            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.IsTrue(string.IsNullOrEmpty(result.ViewName));
            Assert.IsTrue(result.Model is EditOptionTypeViewModel);
            Assert.AreEqual((result.Model as EditOptionTypeViewModel).Name, optionType.Name);
        }

        [TestMethod]
        public virtual void OptionTypeController_PostEditTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<OptionTypeController>());
            OptionType optionType = OptionTypeServiceUtil.CreateOptionType();
            optionType.Name = "Test";

            EditOptionTypeViewModel viewModel = Mapper.Map<OptionType, EditOptionTypeViewModel>(optionType);
            controller.Edit(viewModel);

            var result = controller.Edit(optionType.Id) as ViewResult;
            Assert.IsNotNull(result);
            Assert.AreEqual((result.Model as EditOptionTypeViewModel).Name, "Test");
        }

        [TestMethod]
        public virtual void OptionTypeController_DetailTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<OptionTypeController>());
            OptionType optionType = OptionTypeServiceUtil.CreateOptionType();

            var result = controller.Details(optionType.Id) as ViewResult;

            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.IsTrue(string.IsNullOrEmpty(result.ViewName));
            Assert.IsTrue(result.Model is DetailsOptionTypeViewModel);
            Assert.AreEqual((result.Model as DetailsOptionTypeViewModel).Name, optionType.Name);
        }

		/* Details region test */
        [TestMethod]
        public virtual void OptionTypeController_LoadOptionsTest()
        {
            Option options = OptionServiceUtil.CreateOptionFull();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<OptionTypeController>());

            var result = controller.LoadOptions("", "", 0, 10, "", 1, options.OptionType.Id) as JsonResult;

            var rows = ModelHelper.GetList(result.Data);
            Assert.IsTrue(rows.Count > 0);
        }
        
        [TestMethod]
        public virtual void OptionTypeController_AddOptionsTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<OptionTypeController>());
            Option options = OptionServiceUtil.GetOptionFull();
            PopUpOptionViewModel viewModel = Mapper.Map<Option, PopUpOptionViewModel>(options);

            var result = controller.AddOptions(viewModel) as ContentResult;

            Assert.IsNotNull(result);
            Assert.AreEqual(result.Content, "OK");
        }

        [TestMethod]
        public virtual void OptionTypeController_GetEditOptionsTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<OptionTypeController>());
            Option options = OptionServiceUtil.CreateOptionFull();

            var result = controller.EditOptions(options.Id, options.OptionType.Id) as PartialViewResult;

            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.AreEqual(result.ViewName, "~/Views/Option/PopUp.cshtml");
            Assert.IsTrue(result.Model is PopUpOptionViewModel);
			Assert.AreEqual((result.Model as PopUpOptionViewModel).Name, options.Name);
            
        }

        [TestMethod]
        public virtual void OptionTypeController_PostEditOptionsTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<OptionTypeController>());
            Option options = OptionServiceUtil.CreateOptionFull();
            options.Name = "Test";

            PopUpOptionViewModel viewModel = Mapper.Map<Option, PopUpOptionViewModel>(options);
            controller.EditOptions(viewModel);

            var result = controller.EditOptions(options.Id) as PartialViewResult;
            Assert.IsNotNull(result);
			Assert.AreEqual((result.Model as PopUpOptionViewModel).Name, "Test");
        }

        [TestMethod]
        public virtual void OptionTypeController_DeleteOptionsTest()
        {
            Option options = OptionServiceUtil.CreateOptionFull();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<OptionTypeController>());

            controller.DeleteOptions(options.Id, options.OptionType.Id);

            Assert.AreEqual(controller.Response.StatusCode, 200);
        }

    }
}

